package org.xshop.web.biz;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xshop.model.TSeller;
import org.xshop.model.TSellerBank;
import org.xshop.model.TSellerBrand;
import org.xshop.model.TSellerBusiness;
import org.xshop.model.TSellerLicense;
import org.xshop.service.ITSellerService;
import org.xshop.service.SysService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.BaseController;
import top.ibase4j.core.util.UploadUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 商家  前端控制器
 * </p>
 *
 * @author ShenHuaJie123
 * @since 2018-03-01
 */
@RestController
@RequestMapping("/seller")
@Api(value = "商家接口", description = "商家接口")
public class TSellerController extends BaseController<TSeller, ITSellerService> {
    @Autowired
    private SysService sysService;

    @RequiresPermissions("seller.read")
    @PutMapping(value = "/read/list")
    @ApiOperation(value = "查询商家", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object query(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        return super.query(param);
    }

    @RequiresPermissions("seller.read")
    @PutMapping(value = "/read/detail")
    @ApiOperation(value = "商家详情", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object get(ModelMap modelMap, HttpServletRequest request) {
        TSeller param = WebUtil.getParameter(request, TSeller.class);
        TSeller result = service.queryById(param.getId());
        TSellerLicense license = result.getLicense();
        TSellerBank bank = result.getBank();
        if (license != null) {
            if (StringUtils.isNotBlank(license.getLicenseCountry())) {
                List<?> list = sysService.getDics("PROVINCE", "COUNTRY", license.getLicenseCountry());
                modelMap.put("licenseProvinces", list);
            }
            if (StringUtils.isNotBlank(license.getLicenseProvince())) {
                List<?> list = sysService.getDics("CITY", "PROVINCE", license.getLicenseProvince());
                modelMap.put("licenseCitys", list);
            }
            if (StringUtils.isNotBlank(license.getLicenseCity())) {
                List<?> list = sysService.getDics("AREA", "CITY", license.getLicenseCity());
                modelMap.put("licenseAreas", list);
            }
            if (StringUtils.isNotBlank(license.getSellerCountry())) {
                List<?> list = sysService.getDics("PROVINCE", "COUNTRY", license.getSellerCountry());
                modelMap.put("sellerProvinces", list);
            }
            if (StringUtils.isNotBlank(license.getSellerProvince())) {
                List<?> list = sysService.getDics("CITY", "PROVINCE", license.getSellerProvince());
                modelMap.put("sellerCitys", list);
            }
            if (StringUtils.isNotBlank(license.getSellerCity())) {
                List<?> list = sysService.getDics("AREA", "CITY", license.getSellerCity());
                modelMap.put("sellerAreas", list);
            }
        }
        if (bank != null) {
            if (StringUtils.isNotBlank(bank.getBankCountry())) {
                List<?> list = sysService.getDics("PROVINCE", "COUNTRY", bank.getBankCountry());
                modelMap.put("brankProvinces", list);
            }
            if (StringUtils.isNotBlank(bank.getBankProvince())) {
                List<?> list = sysService.getDics("CITY", "PROVINCE", bank.getBankProvince());
                modelMap.put("brankCitys", list);
            }
            if (StringUtils.isNotBlank(bank.getBankCity())) {
                List<?> list = sysService.getDics("AREA", "CITY", bank.getBankCity());
                modelMap.put("brankAreas", list);
            }
        }
        List<TSellerBusiness> businesses = result.getBusinesses();
        if (businesses != null && !businesses.isEmpty()) {
            Map<?, ?> mainMap = sysService.getDics("MAINBUSINESS");
            Map<?, ?> subMap = sysService.getDics("SUBBUSINESS");
            for (TSellerBusiness business : businesses) {
                business.setTypeITxt((String)mainMap.get(business.getTypeI()));
                business.setTypeIiTxt((String)subMap.get(business.getTypeIi()));
            }
        }
        return setSuccessModelMap(modelMap, result);
    }

    @PostMapping
    @RequiresPermissions("seller.update")
    @ApiOperation(value = "修改商家", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request) {
        TSeller param = WebUtil.getParameter(request, TSeller.class);

        String filePath = UploadUtil.getUploadDir(request);
        if (param.getLicense() != null) {
            String name = param.getLicense().getEleIdCard();
            if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                param.getLicense().setEleIdCard(path);
            }
            name = param.getLicense().getEleLicense();
            if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                param.getLicense().setEleLicense(path);
            }
        }
        if (param.getOrganization() != null) {
            String name = param.getOrganization().getEleCertificate();
            if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                param.getOrganization().setEleCertificate(path);
            }
        }
        if (param.getTaxCertificate() != null) {
            String name = param.getTaxCertificate().getEleCertificate();
            if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                param.getTaxCertificate().setEleCertificate(path);
            }
            name = param.getTaxCertificate().getEleTaxpayerCertificate();
            if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                param.getTaxCertificate().setEleTaxpayerCertificate(path);
            }
        }
        if (param.getBank() != null) {
            String name = param.getBank().getEleAccountLicence();
            if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                param.getBank().setEleAccountLicence(path);
            }
        }
        if (param.getBrands() != null) {
            for (TSellerBrand brand : param.getBrands()) {
                String name = brand.getBrandLogo();
                if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                    String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                    brand.setBrandLogo(path);
                }
            }
        }
        return super.update(param);
    }

    @DeleteMapping
    @RequiresPermissions("seller.delete")
    @ApiOperation(value = "删除商家", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object del(HttpServletRequest request) {
        TSeller param = WebUtil.getParameter(request, TSeller.class);
        Assert.notNull(param.getId(), "ID不能为空");
        return super.delete(param);
    }
}
